(function( $, undefined ) {

$.widget("ui.ajajform_accordion",$.ui.ajajform_widget, {
	options: {
		// \type cssclass
		cssclass: "afw-accordion",
		// \type text
		mode: "accordion",
		// \type array
		pages: []/*,
		// \type text
		htmltemplate_accordion: '<div></div>',
		htmltemplate_accordion_handle: '<h3 class=""><a href=""></a></h3>',
		htmltemplate_accordion_body: '<div></div>',
		htmltemplate_tabs: '<div><ul></ul></div>',
		htmltemplate_tabs_handle: '<li><a href="#tabs-1">Nunc tincidunt</a></li>',
		htmltemplate_tabs_body: '<div id="tabs-1"></div>'*/
	},
	i18n: {
		ru: {next:"Далее",prev:"Назад"},
		en: {next:"Next",prev:"Prev"}
	},
	_create: function() {
		this.element.addClass(this.options.cssclass);
		var t=this;
		this["createLayout_"+this.options.mode]();
		for (var i=0;i<this.options.pages.length;i++) {
			this.renderTab(this.options.pages[i]);
		}
	},

	renderTab: function(page) {
		this["addTabHelper_"+this.options.mode](page);
	},

	createLayout_accordion:function() {
		this.blocks=$("<div class='"+this.options.mode+"'></div>").appendTo(this.element);

/*		this.el=$("<div></div>").appendTo(this.element).addClass("accordion-resize");
		this.el.accordion();*/
	},

	addTabHelper_accordion: function(page) {
		var block=$("<div class='block'></div>");
		$("<div class='block-name block-switch'>"+page.name+"</div>").appendTo(block);
//		$("<div class='block-switch'></div>").appendTo(block);
		$("<div id='"+page.id+"' class='"+page.id+" block-body'></div>").appendTo(block);
		block.appendTo(this.blocks);
		$(".block-switch",block).bind("click",function() {
			$(this).toggleClass("block-switchA");
			$(".block-body",block).toggle();
		});
		if (page.show) {
			$(".block-switch",block).toggleClass("block-switchA");
		} else {
			$(".block-body",block).toggle();
		}
/*		var h3=$('<h3><a></a></h3>').appendTo(this.el);
		$("a",h3).prop("id","#"+page.id);
		$("a",h3).text(page.name);
		var div=$("<div></div>").appendTo(this.el);
		div.addClass(page.id);*/
	},

	createLayout_tabs: function() {
		this.tabswidget=$("<div></div").appendTo(this.element);
		$("<ul></ul>").appendTo(this.tabswidget);
		this.tabswidget.tabs({});
	},
	addTabHelper_tabs: function(page) {
		if (page.hidden) {
			$("<div id='"+page.id+"' class='"+page.id+"' style='display:none;'></div>").appendTo(this.element);
		} else {
			this.tabswidget.tabs( "add","#"+page.id,page.name);
			$("#"+page.id,this.tabswidget).addClass(page.id);
		}
	},

	createLayout_wizard:function() {
		var t=this;
		this.createLayout_blocks();
		var b=$("<div class='wizard-buttons'><input type='button' class='prev' value=''/><input type='button' class='next' value=''/></div>").appendTo(this.blocks);
		$(".next",b).attr("value",this.gettext("next")).button().bind("click",function() {
			t.setCurrentTab(t.currentTab()+1);
		});
		$(".prev",b).attr("value",this.gettext("prev")).button({disabled:true}).bind("click",function() {
			t.setCurrentTab(t.currentTab()-1);
		});
		this.options.currentTab=0;
	},
	addTabHelper_wizard: function(page) {
		var cnt=$(".block",this.blocks).length;
		var block=this.addTabHelper_block_helper(page).appendTo(this.blocks);
		if (cnt==0) block.addClass('blockA');
	},

	createLayout_blocks:function() {
		this.blocks=$("<div class='"+this.options.mode+"'></div>").appendTo(this.element);
	},
	
	addTabHelper_blocks: function(page) {
		this.addTabHelper_block_helper(page).appendTo(this.blocks);
	},

	addTabHelper_block_helper: function(page) {
		var block=$("<div class='block'></div>");
		$("<div class='block-name'>"+page.name+"</div>").appendTo(block);
		$("<div id='"+page.id+"' class='"+page.id+" block-body'></div>").appendTo(block);
		return block;
	},

	addTab: function(tab) {
		this.options.pages.push(tab);
		this.renderTab(tab);
	},

	deleteTab: function(i) {
	},

	tabs: function() {
		return this.options.pages;
	},

	setCurrentTab: function(i) {
		$(".block",this.element).removeClass('blockA');
		$(".block:eq("+i+")",this.element).addClass('blockA');
		this.options.currentTab=i;
		$(".prev",this.element).button("option","disabled",(i==0)).removeClass("ui-state-hover");
		$(".next",this.element).button("option","disabled",(i==this.options.pages.length-1)).removeClass("ui-state-hover");
	},
	currentTab: function() {
		return this.options.currentTab;
	}
});

})( jQuery );
